<template>
  <div style="margin-top: 20px">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="150" class="demo-ruleForm">
      <el-form-item prop="name">
        <el-row :gutter="2">
          <el-col :span="2">起个名字吧:</el-col>
          <el-col :span="5">
            <el-input v-model="ruleForm.spsname" placeholder="起个容易记的名字！"></el-input>
          </el-col>
        </el-row>
      </el-form-item>
      <!--      类别-->
      <el-form-item label="鞋子类别:">
        <el-select v-model="ruleForm.tid" placeholder="请选择">
          <el-option v-for="item in typeOptions" :key="item.tid" :label="item.tname" :value="item.tid">
          </el-option>
        </el-select>
      </el-form-item>
      <!--      牌子-->
      <el-form-item label="选择品牌:" align="left" header-align="center">
        <el-select v-model="ruleForm.bid" placeholder="请选择">
          <el-option v-for="item in brandOptions" :key="item.bid" :label="item.bname" :value="item.bid">
          </el-option>
        </el-select>
      </el-form-item>
      <!--      价格-->
      <el-form-item label="鞋子价位" prop="spspricesString">
        <el-radio-group v-model="ruleForm.spspricesString">
          <el-radio label="0 ~ 50元"></el-radio>
          <el-radio label="50 ~ 150元"></el-radio>
          <el-radio label="150 ~ 500元"></el-radio>
          <el-radio label="500 ~ 1000元"></el-radio>
          <el-radio label="1000 ~ 6000元"></el-radio>
        </el-radio-group>
      </el-form-item>
      <!--部件信息-->
      <el-form-item label="部件信息:(默认一双鞋，鞋垫两个，鞋带两个)" prop="spspartinfo">
        <el-checkbox-group v-model="ruleForm.spspartinfo">
          <el-checkbox label="鞋子" checked disabled name="type"></el-checkbox>
          <el-checkbox label="鞋带" checked name="type"></el-checkbox>
          <el-checkbox label="鞋垫" checked name="type"></el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <!--部件信息-->
      <el-form-item label="鞋子尺码" prop="size">
        <el-select v-model="ruleForm.size" placeholder="请选择尺码">
          <el-option v-for="item in sizeOptions" :key="item.sizeid" :label="item.sizenum" :value="item.sizenum">
          </el-option>
        </el-select>
      </el-form-item>
      <!--详细信息-->
      <el-form-item label="描述一下想要的类型">
        <el-input v-model="ruleForm.spscontent" placeholder="描述一下吧"></el-input>
      </el-form-item>
      <!--      性别-->
      <el-form-item label="性别" prop="spsgender">
        <el-radio-group v-model="ruleForm.spsgender">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item></el-form-item>
      <el-form-item label="备注">
        <el-input type="textarea" autosize v-model="ruleForm.spremarks" placeholder="有啥需求尽管提！"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')" justify="center">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

export default {
  data() {
    return {
      typeOptions: [],
      brandOptions: [],
      sizeOptions: [],
      user: JSON.parse(localStorage.getItem("user")) ? JSON.parse(localStorage.getItem("user")) : "",
      ruleForm: {
        spsname: '',
        uid: '',
        tid: '',//类型
        bid: '',//品牌
        spspricesString: [],//价格
        spscontent: "",//详细信息
        spspartinfo: [],//部件信息
        spspartnum: "",//部件数量
        spsgender: "",//鞋子性别
        size: '',
        spremarks: ''//备注
      },
      form: {
        spsname: '',
        uid: '',
        spTid: '',//类型id
        spBid: '',//品牌id
        spspricesString: [],//价格
        spscontent: "",//详细信息
        spspartinfo: [],//部件信息
        spspartnum: "",//部件数量
        spsgender: "",//鞋子性别
        size: '',
        spremarks: ''//备注
      },
      rules: {
        spsname: [
          {required: true, message: '请输入定制鞋名称', trigger: 'blur'},
          {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
        ],
        spspricesString: [
          {required: true, message: '请至少选择一个价格区间', trigger: 'change'}
        ],
        size: [{
          required: true, message: '请选择尺码', trigger: 'change'
        }
        ],
        spsgender: [
          {required: true, message: '请选择性别', trigger: 'change'}
        ],
        spspartinfo: [
          {required: true, message: '请选择', trigger: 'change'}
        ],

      }
    };
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      if (!this.user.uid) {
        this.$message({
          type: 'warning',
          message: '请登录！'
        })
        return
      } else {
        this.typeLoad()
        this.brandLoad()
        this.sizeLoad()
      }
    },
    typeLoad() {
      axios.get("/types").then(res => {
        this.typeOptions = res.data.data
      })
    },
    brandLoad() {
      axios.get("/brands").then(res => {
        this.brandOptions = res.data.data
      })
    },
    sizeLoad() {
      axios.get("/sizes").then(res => {
        this.sizeOptions = res.data.data
      })
    },
    submitForm(formName) {
      this.$confirm('将提交订单信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.ruleForm.uid = this.user.uid
            this.form = this.ruleForm
            this.form.spspartinfo = this.ruleForm.spspartinfo.toString()
            console.log(this.form)
            console.log(this.ruleForm)
            axios.post("/spcify-shoes" , this.form).then(res => {
              if (res.data.code === 200) {
                this.$message.success(res.data.msg)
              } else {
                this.$message.error("操作失败")
              }
            })
          } else {
            this.$message.error("提交失败，表格不符合规范")
            return false;
          }
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消提交'
        });
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
}
</script>

<style>

</style>